import 'package:wu_you/data/api/api.dart';
import 'package:wu_you/http/http.dart';
import 'package:wu_you/model/user/team_model.dart';
import 'package:wu_you/model/user/user_model.dart';

class UserRepository{
  factory UserRepository() => _getInstance();
  static UserRepository _instance;

  UserRepository._internal();

  static UserRepository _getInstance() {
    if (_instance == null) {
      _instance = UserRepository._internal();
    }
    return _instance;
  }

  ///用户信息
  Future<UserInfoModel> getUserInfo() async {
    Map<String, dynamic> mapResult = await Http().get(Api.userInfoUrl);
    UserInfoModel userInfo = UserInfoModel.fromJson(mapResult);
    return userInfo;
  }

  ///团队
  Future<TeamInfoModel> getUserTeam() async {
    Map<String, dynamic> mapResult = await Http().get(Api.userTeam);
    TeamInfoModel team = TeamInfoModel.fromJson(mapResult);
    return team;
  }

  ///获取收货地址列表
  Future<List<AddressModel>> getAddressList() async {
    List<dynamic> mapResult = await Http().get(Api.addressList);
    List<AddressModel> list = [];
    if(mapResult != null){
       list = mapResult.map((e) => AddressModel.fromJson(e)).toList();
    }
    return list;
  }

  ///新增收货地址
  Future<dynamic> addReceiveAddress(String name,String mobile,String address) async {
    Map<String, dynamic> data = {
      "name":name,
      "mobile":mobile,
      "detailed_address":address,
    };
    return await Http().post(Api.addAddress,data: data);
  }

  ///设置默认地址
  Future<dynamic> updateReceiveAddress({String id}) async {
    Map<String, dynamic> data = {
      "id": id,
    };
    return await Http().post(Api.setDefaultAddress, data: data);
  }

  ///删除收货地址
  Future<dynamic> deleteReceiveAddress(String id) async {
    return await Http().get(Api.deleteAddress,params: {"id":id});
  }

  ///获取默认收货地址
  Future<AddressModel> getDefaultAddress() async {
    dynamic mapResult = await Http().get(Api.getDefaultAddress);

    if(mapResult  != null){
      return AddressModel.fromJson(mapResult);
    }else{
      Map<String,dynamic> err = {

      };
      return AddressModel.fromJson(err);
    }
  }

  ///修改登录密码
  Future<dynamic> changeLoginPwd(String password,String code) async {
    Map<String, dynamic> data = {
      "password":password,
      "confirmPasswor":password,
      "code":code,
    };
    return await Http().post(Api.changeLoginPwd,data: data);
  }

  ///设置交易密码
  Future<dynamic> setTradePassword(String password,String code) async {
    Map<String, dynamic> data = {
      "pay_password":password,
      "confirmPasswor":password,
      "code":code,
    };
    dynamic mapResult = await Http().post(Api.setTradePwd,data: data);
    return mapResult;
  }

  ///一键领取奖金
  Future<dynamic> getTeamReward() async {
    dynamic mapResult = await Http().post(Api.teamGetReward);
    return mapResult;
  }

  ///获取推广码
  Future<dynamic> getShareCode() async {
    dynamic mapResult = await Http().get(Api.shareCode);
    return mapResult;
  }

  ///获取流水信息
  Future<List<FundsListModel>> getFundsList({String genre,int page,int limit}) async {
    Map<String,dynamic> data = {
      "genre":genre,
      "page":page,
      "limit":limit
    };
    dynamic mapResult = await Http().get(Api.fundsList,params: data);
    List<dynamic> list = mapResult['list'];
    List<FundsListModel> fl = [];
    if(list != null){
        fl = list.map((e) => FundsListModel.fromJson(e)).toList();
    }
    return fl;
  }

  ///待释放积分列表
  Future<List<PointsSetModel>> getPointsList() async {
    List<dynamic> mapResult = await Http().post(Api.pointsSet);
    List<PointsSetModel> fl = [];
    if(mapResult != null){
      fl = mapResult.map((e) => PointsSetModel.fromJson(e)).toList();
    }
    return fl;
  }

  ///销售奖领取
  Future<dynamic> getSaleReward() async {
    dynamic mapResult = await Http().post(Api.userSaleReward,showExtraInfo: true);
    return mapResult;
  }

  ///中奖福利领取
  Future<dynamic> getWinningReward() async {
    dynamic mapResult = await Http().post(Api.userWinningReward,showExtraInfo: true);
    return mapResult;
  }

  ///周分红领取
  Future<dynamic> getWeekReward() async {
    dynamic mapResult = await Http().post(Api.userWeekReward,showExtraInfo: true);
    return mapResult;
  }

  ///滑落码
  Future<dynamic> getHualuoCode(String username) async{
    dynamic result = await Http().post(Api.hualuo, data: {"username":username});
    return result;
  }

  ///绑定姓名电话
  Future<dynamic> bindRealName(String truename,String phone) async{
    dynamic result = await Http().post(Api.bindNp, data: {"truename":truename,"phone":phone});
    return result;
  }

  ///转账
  Future<dynamic> transferMoney(String username,String cny,String code,String type) async{
    dynamic result = await Http().post(Api.transferMoney, data: {"username":username,"cny":cny,"code":code,"type":type});
    return result;
  }

  ///转账
  Future<dynamic> exchangeMoney(String nums) async{
    dynamic result = await Http().post(Api.exchangeApi, data: {"nums":nums});
    return result;
  }

  ///提现信息
  Future<WithdrawInfo> getWithdrawInfo() async {
    dynamic mapResult = await Http().post(Api.withdrawInfo);
    WithdrawInfo wi = WithdrawInfo.fromJson(mapResult);
    return wi;
  }

  ///提现
  Future<dynamic> txApply(String money,String code,String type) async {
    dynamic mapResult = await Http().post(Api.withdraw,data: {"money":money,"code":code,"type":type});
    return mapResult;
  }

  ///提现记录
  Future<List<WithdrawRecord>> getWithdrawList({int page,int limit}) async {
    List<dynamic> mapResult = await Http().post(Api.withdrawList,data: {"page":page,"limit":limit});
    List<WithdrawRecord> wr = [];
    if(mapResult != null){
      wr = mapResult.map((e) => WithdrawRecord.fromJson(e)).toList();
    }
    return wr;
  }
}
